<template>
  <d2-container>
  <div class="mod-config">
    <div class="search">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList(true)" size="small">
        <el-form-item>
          <el-input v-model="dataForm.key" placeholder="指标名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList(true)">查询</el-button>
          <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
          <el-button v-if="isAuth('ecs:ecsquotainfo:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%; margin-top:15px;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
        <el-table-column prop="quotaName" label="指标名称" minWidth="160" align="center"></el-table-column>
        <el-table-column prop="normal" label="额定值/增幅值" minWidth="150" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.quotaType==0">额定值</span>
            <span v-if="scope.row.quotaType==1">增幅值</span>
          </template>
        </el-table-column>
        <el-table-column prop="upVal" label="上限" minWidth="100" align="center"></el-table-column>
        <el-table-column prop="downVal" label="下限" minWidth="100" align="center"></el-table-column>
        <el-table-column prop="addedVal" label="增幅值" minWidth="100" align="center"></el-table-column>
        <el-table-column prop="routine_type" label="指标周期" minWidth="100" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.quotaUnit==1">{{scope.row.quotaCycle}}(分钟)</span>
            <span v-if="scope.row.quotaUnit==2">{{scope.row.quotaCycle}}(小时)</span>
            <span v-if="scope.row.quotaUnit==3">{{scope.row.quotaCycle}}(天)</span>
            <span v-if="scope.row.quotaUnit==4">{{scope.row.quotaCycle}}(月)</span>
            <span v-if="scope.row.quotaUnit==5">{{scope.row.quotaCycle}}(年)</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" show-overflow-tooltip min-width="250" align="center">
          <template slot-scope="scope">
            <el-button type="success" icon="eye" size="mini" @click="viewItem(false,scope.row,)">查看指标对象</el-button>
            <el-button type="success" icon="edit" size="mini" @click="modifyItem(false,scope.row)">修改</el-button>
            <el-button type="danger" icon="delete" size="mini" @click="deleteHandle(scope.row.quotaId)">删除</el-button>
          </template>
        </el-table-column>
    </el-table>
    <el-pagination
      class="mt-4"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="page.page_no"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="page.page_size"
      :total="page.total"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <view-ecs-info v-if="viewVisible" ref="viewEcsInfo"></view-ecs-info>
  </div>
  </d2-container>
</template>

<script>
  import AddOrUpdate from './ecsquotainfo-add-or-update'
  import ViewEcsInfo from './ecsquotainfo-view'
  import commondef from '@/utils/common'
  import * as Util from "@/utils";
  export default {
    data () {
      return {
        meterTypeParamValueList:[],
        dataForm: {
          key: ''
        },
        dataList: [],
        page: {
          page_size: commondef.globlePagesize,
          total: null,
          page_no: 1
        },
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        viewVisible:false,
      }
    },
    computed:{
      containerHeight() {
        return Util.getClientHeight() - 300;
      },
    },
    filters: {
      statusName: function (value,statusList) {
        for(let i=0;i<statusList.length;i++){
          if(statusList[i].collectType===value){
            return statusList[i].collectName;
          }
        }
      },
    },
    components: {
      AddOrUpdate,
      ViewEcsInfo
    },
    mounted () {
      this.getDataList()
      //this.querySubitem('')
    },
    methods: {
      // 获取数据列表
      getDataList (type) {
        if(type){
          this.pageIndex = 1
        }
        this.dataListLoading = true
        this.$api.okhttp({
          url: this.$http.adornUrl('/ecs/ecsquotainfo/list'),
          method: 'get',
          params: this.$http.adornParams({
            page: this.page.page_no,
            limit: this.page.page_size,
            quotaName: this.dataForm.key,
            customerId: this.$util.cookies.get("customer_id"),
            projectid:this.$util.cookies.get("cuuren_projectId")
          })
        }).then((data) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.page.page_size = val
        this.page.page_no = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.page.page_no = val
        this.getDataList()
      },
      viewItem(flag,row){
        this.viewVisible = true;
        this.$nextTick(()=>{
          this.$refs.viewEcsInfo.init(row.quotaId);
        })
      },
      modifyItem(flag,row){
        this.addOrUpdateVisible = true;
        this.$nextTick(()=>{
          this.$refs.addOrUpdate.init(row.quotaId);
        })
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.quotaId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$api.okhttp({
            url: this.$http.adornUrl('/ecs/ecsquotainfo/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then((data) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      querySubitem(meterType){
        //查询子分类
        this.queryCollectType(meterType,'0',data=>{
          if (data && data.code === 0) {
          this.meterTypeParamValueList = data.page.list
        } else {
          this.meterTypeParamValueList = [];
        }
      });
      },
      /**
       * 获取采集分项
       */
      queryCollectType(type,paramValue,callback) {
        let params = {
          'meterType': type ,
          'state': '1' ,
          'cumulativeType':paramValue,
          "customerId":this.$util.cookies.get('customer_id'),
          'page': 1,
          'limit': 10000,
        }
        this.$api.okhttp({
          url: this.$http.adornUrl('/ecs/ecscollectitem/list'),
          method: 'get',
          params: this.$http.adornParams(params)
        }).then((data) => {
          if(callback){
            callback(data);
          }
        });
      },
    }
  }
</script>
